package danliebiao;

import java.util.Iterator;
import java.util.function.Consumer;

//单项链表
public class SinglyLinkedList implements Iterable<Integer> {  //整体
    private Node head = null;//头指针

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public Integer next() {
                return null;
            }

            SinglyLinkedList.Node p = head;


        };
    }

    //节点类
    private static class Node {
        int value; //值
        Node next; //下一个节点指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    public void addFirst(int value) {
        //链表为空
        // head = new Node(value, null);

        //链表非空
        head = new Node(value, head);
    }

    public void loop() {
        Node p = head;
        while (p != null) {
            System.out.println(p.value);
            p = p.next;
        }
    }

    public void loop1(Consumer<Integer> consumer) {
        Node p = head;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head; p != null; p = p.next) {
            consumer.accept(p.value);

        }
    }

    private Node findLast() {
        if (head == null) {
            return null;
        }
        Node p;
        for (p = head; p != null; p = p.next) {

        }
        return p;
    }

    public void addLast(int value) {

        Node last = findLast();
        if(last == null) {
            addFirst(value);
            return ;
        }
        last.next = new Node(value, null);

    }
}




